<?php
/**
 * Pipahr
 *
 * @author mawei
 * @copyright 2013 pipahr.com
 */
JLoader::register("PPersontestModelPersontest", OC::$SERVERROOT."/apps/interview/lib/persontest.php");
JLoader::register("PCompanyHelper", OC::$SERVERROOT."/apps/company/lib/helper.php");

class PPersontestController
{
   // home page view
   public static function home() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_login"));
         return;
      }
      
      if(!PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_home"));
         return;
      }
      
      $app = PFactory::getApplication();
      
      $jobid = JRequest::getInt('jobid', 0);
      $sheetid = JRequest::getInt('sheetid', 0);
      if(empty($jobid)&&empty($sheetid)){
         OC_Response::redirect(OC_Helper::linkToRoute("interview_error"));
         return;
      }
      $model = new PPersontestModelPersontest();
      if($jobid){
         $sheetid=$model->getSheetidByjobid($jobid);
      }
      $hasDone = $model->hasDone($sheetid);
      $hasDelivered = $model->hasDelivered($jobid,$sheetid);
      $persontestInfo = $model->getPersontestInfo($jobid,$sheetid);
      if(empty($persontestInfo) || !$hasDelivered || $hasDone) {
         OC_Response::redirect(OC_Helper::linkToRoute("interview_error"));
         return;
      }
      
      $persontestQuestions = $model->getPersontestQuestions($jobid,$sheetid);     
      $persontest_start_time = $app->getUserState('persontest_start_time_' . $sheetid, null);
      $test_flag=true;
      if(empty($persontest_start_time)) { 
         $persontest_start_time = PFactory::getDate()->toSql();
         $app->setUserState('persontest_start_time_' . $sheetid, $persontest_start_time);
         $test_flag=false;                 
      }
      $tmpl = new OCP\Template('interview', 'persontest','base'); 
      $tmpl->assign('test_flag', $test_flag);
      $tmpl->assign('persontestInfo', $persontestInfo);
      $tmpl->assign('sheetid', $sheetid);
      $tmpl->assign('page_title', '性格评测');
      $tmpl->assign('persontestQuestions', $persontestQuestions);
      $tmpl->printPage(); 
   }
   
   public static function save() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_login"));
         return;
      }
      
      if(!PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_home"));
         return;
      }
      
      $jobid = JRequest::getInt('jobid', 0);
      $sheetid = JRequest::getInt('sheetid', 0);
      $persontestInfo = array('jobid' => $jobid,'sheetid' => $sheetid);
      
      $app = PFactory::getApplication();
      $app->setUserState('persontestInfo', $persontestInfo);
      
      $model = new PPersontestModelPersontest();
      
      if (!$model->save()) {
         jexit(jsonError($model->getError(), null, false));
      }
      
      jexit(jsonSuccess('success', OC_Helper::linkToRoute("persontest_success"), null, false));
   }
   
   // submit success page
   public static function success() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_login"));
         return;
      }
      
      if(!PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_home"));
         return;
      }
      
      $app = PFactory::getApplication();
      $persontestInfo = $app->getUserState('persontestInfo');
      $jobid = isset($persontestInfo['jobid']) ? $persontestInfo['jobid'] : 0;
      $sheetid = isset($persontestInfo['sheetid']) ? $persontestInfo['sheetid'] : 0;
      if (empty($persontestInfo)) {
         OC_Response::redirect(OC_Helper::linkToRoute("interview_error"));
         return;
      }
   
      $model = new PPersontestModelPersontest;
      $jobposInfo = $model->getJobposInfo($jobid);
   
      $tmpl = new OCP\Template('interview', 'persontestsuccess', 'base');
      $tmpl->assign('jobposInfo', $jobposInfo);
      $tmpl->assign('sheetid', $sheetid);
      $tmpl->assign('page_title', '提交成功');
      $tmpl->printPage();
   }
 
}
